package cn.com.byte1024.controller;

import cn.com.byte1024.entity.VoteProject;
import cn.com.byte1024.service.VoteProjectService;
import cn.com.byte1024.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ProjectName: VoteSystem
 * @ClassName: VoteProjectController
 * @Description:
 * @Author: Kwnda
 * @Date: 2021/1/5 14:38
 */
@Controller
@RequestMapping("/voteProject")
public class VoteProjectController {
    
    @Autowired
    private VoteProjectService voteProjectService;
    
    
    @RequestMapping("/goVoteProjectJsp")
    public String goVoteProjectJsp(@RequestParam("userId") String userId, Model model){
        getVoteProject(userId,model);
        return "vote-project";
    }

    @ResponseBody
    @RequestMapping("/addVoteProjectByProjectCode")
    public String addVoteProjectByProjectCode(@RequestParam("projectCode") String projectCode, @RequestParam("userId") String userId,Model model){
        boolean flag = voteProjectService.addVoteProjectByProjectCode(projectCode,userId);
        if(flag){
            getVoteProject(userId,model);
            return "success";
        }else {
            return "error";
        }
      
    }

    @ResponseBody
    @RequestMapping("/delete")
    public String delete(@RequestParam("voteProjectId") String voteProjectId,@RequestParam("userId") String userId,Model model){
        //根据项目id删除
        //如果是项目发起者删除项目，则将项目删除，投票删除
        //如果是普通用户，则提示没有权限删除
        boolean flag = voteProjectService.deleteByVoteProject(voteProjectId,userId);
        getVoteProject(userId,model);
        if(flag){
            return "success";
        }else{
            return "error";
        }
    }
    
    @RequestMapping("/add")
    public String add(){
        return "vote-project-add";
    }

    @RequestMapping("/edit")
    public String edit(@RequestParam("voteProjectId") String voteProjectId,Model model){
        VoteProject voteProject = voteProjectService.selectByPrimaryKey(voteProjectId);
        model.addAttribute("voteProject",voteProject);
        return "vote-project-edit";
    }
    
    @ResponseBody
    @RequestMapping("/openEdit")
    public String openEdit(@RequestParam("voteProjectId") String voteProjectId,HttpServletRequest request){
        String userId = request.getSession().getAttribute("userId").toString();
        VoteProject voteProject = voteProjectService.selectByPrimaryKey(voteProjectId);
        if(userId.equals(voteProject.getUserId())){
            return "success";
        }else{
            return "error";
        }
    }

    @ResponseBody
    @RequestMapping("/save")
    public String save(@RequestParam("userId") String userId,@RequestParam("projectName") String projectName, @RequestParam("projectVoteTotal") String projectVoteTotal, @RequestParam("singleVote") String singleVote, @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime, Model model ){
        VoteProject voteProject = new VoteProject();
        voteProject.setVoteProjectId(CommonUtil.generateUUID());
        voteProject.setUserId(userId);
        voteProject.setProjectName(projectName);
        String projectCode = "";
        boolean flag=true;
        while (flag){
            projectCode = CommonUtil.generateCode();
            VoteProject result = voteProjectService.selectByProjectCode(projectCode);
            if(result==null){
                flag = false;
            }
        }
        voteProject.setProjectCode(projectCode);
        voteProject.setProjectVoteTotal(Integer.parseInt(projectVoteTotal));
        voteProject.setSingleVote(Integer.parseInt(singleVote));
        voteProject.setStartTime(startTime);
        voteProject.setEndTime(endTime);

        voteProjectService.insertSelective(voteProject);
        
        getVoteProject(userId,model);
        return "success";
    }

    @ResponseBody
    @RequestMapping("/update")
    public String update(VoteProject voteProject, Model model){
        voteProjectService.updateByPrimaryKeySelective(voteProject);
        getVoteProject(voteProject.getUserId(),model);
        return "success";
    }
    
    
    
    public void getVoteProject(String userId,Model model){
        List<VoteProject> voteProjectList=voteProjectService.selectAllVoteProject(userId);
        int num = voteProjectList.size();
        model.addAttribute("voteProjectList",voteProjectList);
        model.addAttribute("num",num);
    }
}
